package com.foreknow.elm.dao.impl;

import com.foreknow.elm.dao.OrderDetailetDao;
import com.foreknow.elm.dao.OrdersDao;
import com.foreknow.elm.po.Business;
import com.foreknow.elm.po.Cart;
import com.foreknow.elm.po.OrderDetailet;
import com.foreknow.elm.po.Orders;
import com.foreknow.elm.util.DBUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class OrdersDaoImpl implements OrdersDao {
    private Connection con = null;
    private PreparedStatement pst = null;
    private ResultSet rs = null;
    private int a;
    private int orderid;

    @Override
    public int insertOrders(String userId, Integer businessId, Integer daId, Double orderTotal) {
        String sql = "insert into orders(userId,businessId,daId,orderTotal) values(?,?,?,?)";
        try {
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
            pst.setString(1,userId);
            pst.setInt(2,businessId);
            pst.setInt(3,daId);
            pst.setDouble(4,orderTotal);
            a = pst.executeUpdate();
            rs = pst.getGeneratedKeys();
            if(rs.next()) {
                orderid = rs.getInt(1);
            }
//            orderid = getOrdersId(userId, businessId, daId, orderTotal);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close(rs,pst);

        }
        return orderid;
    }

//    @Override
//    public int getOrdersId(String userId, Integer businessId, Integer daId, Double orderTotal) throws SQLException {
//        String sql = "select orderId from orders where userId=? and businessId=? and daId=? and orderTotal=?";
//        try {
//            con = DBUtil.getConnection();
//            pst = con.prepareStatement(sql);
//            pst.setString(1,userId);
//            pst.setInt(2,businessId);
//            pst.setInt(3,daId);
//            pst.setDouble(4,orderTotal);
//            rs = pst.executeQuery();
//            while (rs.next()) {
//                orderid = rs.getInt("orderId");
//            }
//        } catch (SQLException e) {
//            DBUtil.close(rs,pst);
//        }
//        return orderid;
//    }

//    @Override
//    public List<Cart> selectCart(String userId, Integer businessId) {
//        String sql ="select * from cart where userId=? and businessId=?";
//        List<Cart> list = new ArrayList<>();
//        try {
//            con = DBUtil.getConnection();
//            pst = con.prepareStatement(sql);
//            pst.setString(1,userId);
//            pst.setInt(2,businessId);
//            rs = pst.executeQuery();
//            while (rs.next()){
//                Cart cart = new Cart();
//                cart.setCartId(rs.getInt("cartId"));
//                cart.setFoodId(rs.getInt("foodId"));
//                cart.setBusinessId(rs.getInt("businessId"));
//                cart.setUserId(rs.getString("userId"));
//                cart.setQuantity(rs.getInt("quantity"));
//                list.add(cart);
//            }
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }finally {
//            DBUtil.close(rs,pst);
//        }
//        return list;
//    }

//    @Override
//    public int insertOrderDetailet(List<Cart> list) {
//        String sql ="insert into orderdetailet(foodId,quantity) values(?,?)";
//        try {
//            con = DBUtil.getConnection();
//            pst = con.prepareStatement(sql);
//            for (Cart cart : list) {
//                pst.setInt(1,cart.getFoodId());
//                pst.setInt(2,cart.getQuantity());
//                pst.executeUpdate();
//            }
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }finally {
//            DBUtil.close(rs,pst);
//
//        }
//        return list.size();
//    }

//    @Override
//    public int deleteCart(String userId, Integer businessId) {
//        String sql = "delete from cart where userId=? and businessId=?";
//        try{
//            con = DBUtil.getConnection();
//            pst = con.prepareStatement(sql);
//            pst.setString(1,userId);
//            pst.setInt(2,businessId);
//            a = pst.executeUpdate();
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }finally {
//            DBUtil.close(rs,pst);
//
//        }
//        return a;
//    }

    @Override
    public Orders getOrdersById(Integer orderId) throws Exception {
        Orders order = new Orders();
        Business business = new Business();
        String sql = "select * from orders o left join business b on o.businessId=b.businessId left join orderdetailet od on o.orderId=od.orderId where o.orderId=?";
        try {
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql);
            pst.setInt(1,orderId);
            rs = pst.executeQuery();
            while (rs.next()) {
                business.setBusinessId(rs.getInt("businessId"));
                business.setBusinessName(rs.getString("businessName"));
                business.setBusinessAddress(rs.getString("businessAddress"));
                business.setBusinessExplain(rs.getString("businessExplain"));
                business.setBusinessImg(rs.getString("businessImg"));
                business.setOrderTypeId(rs.getInt("orderTypeId"));
                business.setStarPrice(rs.getDouble("starPrice"));
                business.setDeliveryPrice(rs.getDouble("deliveryPrice"));
                business.setRemarks(rs.getString("remarks"));
                order.setOrderId(rs.getInt("orderId"));
                order.setUserId(rs.getString("userId"));
                order.setBusinessId(rs.getInt("businessId"));
                order.setOrderDate(rs.getString("orderDate"));
                order.setOrderTotal(rs.getDouble("orderTotal"));
                order.setDaId(rs.getInt("daId"));
                order.setBusiness(business);
            }
        } finally {
            DBUtil.close(rs,pst);
        }
        return order;
    }

//    @Override
//    public Business getBusinessById(Integer businessId) throws Exception {
//        Business business = new Business();
//        String sql = "select * from business where businessId=?";
//        try {
//            con = DBUtil.getConnection();
//            pst = con.prepareStatement(sql);
//            pst.setInt(1,businessId);
//            rs = pst.executeQuery();
//            while (rs.next()) {
//                business.setBusinessName(rs.getString("businessName"));
//                business.setBusinessAddress(rs.getString("businessAddress"));
//                business.setBusinessExplain(rs.getString("businessExplain"));
//                business.setBusinessImg(rs.getString("businessImg"));
//                business.setOrderTypeId(rs.getInt("orderTypeId"));
//                business.setStarPrice(rs.getDouble("starPrice"));
//                business.setDeliveryPrice(rs.getDouble("deliveryPrice"));
//                business.setRemarks(rs.getString("remarks"));
//            }
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
//        return business;
//    }

//    @Override
//    public List<OrderDetailet> getOrderDetailetById(Integer orderId) throws Exception {
//        List<OrderDetailet> list = new ArrayList<>();
//        String sql = "select * from orderdetailet where orderId=?";
//        try {
//            con = DBUtil.getConnection();
//            pst = con.prepareStatement(sql);
//            pst.setInt(1,orderId);
//            rs = pst.executeQuery();
//            while (rs.next()) {
//                OrderDetailet orderDetailet = new OrderDetailet();
//                orderDetailet.setOdId(rs.getInt("odId"));
//                orderDetailet.setOrderId(rs.getInt("orderId"));
//                orderDetailet.setFoodId(rs.getInt("foodId"));
//                orderDetailet.setQuantity(rs.getInt("quantity"));
//                list.add(orderDetailet);
//            }
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
//        return list;
//    }

    @Override
    public List<Orders> listOrdersByUserId(String userId) throws Exception {
        List<Orders> list = new ArrayList<>();
        String sql = "select * from orders o left join business b on o.businessId=b.businessId left join orderdetailet od on o.orderId=od.orderId where o.userId=?";
        try {
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql);
            pst.setString(1, userId);
            rs = pst.executeQuery();
            while (rs.next()) {
                Orders order = new Orders();
                order.setOrderId(rs.getInt("orderId"));
                order.setUserId(rs.getString("userId"));
                order.setBusinessId(rs.getInt("businessId"));
                order.setOrderDate(rs.getString("orderDate"));
                order.setOrderTotal(rs.getDouble("orderTotal"));
                order.setDaId(rs.getInt("daId"));
                order.setOrderState(rs.getInt("orderState"));
                Business business = new Business();
                business.setBusinessId(rs.getInt("businessId"));
                business.setBusinessName(rs.getString("businessName"));
                business.setBusinessAddress(rs.getString("businessAddress"));
                business.setBusinessExplain(rs.getString("businessExplain"));
                business.setBusinessImg(rs.getString("businessImg"));
                business.setOrderTypeId(rs.getInt("orderTypeId"));
                business.setStarPrice(rs.getDouble("starPrice"));
                business.setDeliveryPrice(rs.getDouble("deliveryPrice"));
                business.setRemarks(rs.getString("remarks"));
                order.setBusiness(business);
                list.add(order);
            }
        } finally {
            DBUtil.close(rs,pst);
        }
        return list;
    }



}